#include "../func.h"

int main(int argc,char** argv)
{
    if(argc!=3)
    {
        printf("error args\n");
        return -1;
    }
    int fdw,fdr;
    fdw=open(argv[1],O_WRONLY);//打开管道1的写端
    if(-1==fdw)
    {
        perror("open1");
        return -1;
    }
    fdr=open(argv[2],O_RDONLY);//打开管道2的读端
    if(-1==fdr)
    {
        perror("open2");
        return -1;
    }
    printf("I can write,fdw=%d,fdr=%d\n",fdw,fdr);
    char buf[20];
    fd_set rdset;
    int ret;
    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(0,&rdset);
        FD_SET(fdr,&rdset);
        ret=select(fdr+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(fdr,&rdset))    
        {
            memset(buf,0,sizeof(buf));
            ret=read(fdr,buf,sizeof(buf));//读取管道
            if(ret>0)
            {
                printf("A:%s\n",buf);
            }else{
                printf("byebye\n");
                break;
            }
        }
        if(FD_ISSET(0,&rdset))
        {
            memset(buf,0,sizeof(buf));
            ret=read(0,buf,sizeof(buf));//读取标准输入
            if(ret>0)
            {
                write(fdw,buf,strlen(buf)-1);
            }else{
                printf("byebye\n");
                break;
            }
        }
    }
    close(fdr);
    close(fdw);
    return 0;
}

